$(document).ready(function(){
//更换验证码
	$(".imgCodeCl").click(function(){
		var codeSrc = $(this).attr("src");
		var rum = getRandom(100);
		if($(this).attr("id")=='emailImage') {
			$("#emlRegImgCodeId").val(rum);
		} else {
			$("#mobRegImgCodeId").val(rum);
		}
		codeSrc=codeSrc.substring(0,codeSrc.lastIndexOf("/"))+"/"+rum;
		$(this).attr("src",codeSrc);
	});
	function getRandom(n){
    	return Math.floor(Math.random()*n+1)
    }
	/**
	发送验证信息入口*/
	$("#sendMObileMess").click(function(){
		smsSend();
	});
	$("#sendEmailMess").click(function(){
		emailSend();
	});
});

/**
 * 发送短信验证码
 */
var smsSend=function(id){
	var ischeck = 1;
	if(document.getElementById("ischeck")!=undefined){
		ischeck=2;
	};
	var phone = $("#mobile").val();
	if(ischeck == 1){
		var imgcode = $("#imgcodeMobile").val();
		if(phone==null || phone.length==0) {
			showModal('请输入正确格式的手机号码');
			$("#mobile").addClass("input-error");
			return false;
		} else if(phone.length==11) {
			$("#mobile").removeClass("input-error");
		} 
		if(imgcode==null || imgcode.length==0) {
			showModal('请填写图形验证码');
			$("#imgcodeMobile").addClass("input-error");
			return false;
		} else if(imgcode.length==5) {
			$("#imgcodeMobile").removeClass("input-error");
		}
	}else{
		if(phone==null || phone.length==0) {
			showModal('请先设置手机号');
			return false;
		}; 
	};
	var smsurl=$("#sendMobileUrl").val()+"?mobile="+phone+"&imgcode="+imgcode+"&imgCodeId="+$("#mobRegImgCodeId").val();;
	$.ajax({
		type:'get',
		url:smsurl,
		success:function(data){
			if(data.code=='error') {
				showModal(data.info);
			} else {
				fun_timedown(60);
				showModal(data.info);
				$("#sendMObileMess").addClass("button-disable");
				$("#sendMObileMess").unbind("click");
			}
		}
	});
};
function fun_timedown(time) {
	$("#sendMObileMess").text(time+"秒");
    time = time-1;
    if(time>=0){
        setTimeout("fun_timedown("+time+")",1000);
    }else{
    	$("#sendMObileMess").removeClass("button-disable");
    	$("#sendMObileMess").click(function(){
			smsSend();
    	});
    	$("#sendMObileMess").text("发送验证码");
    }
}

/**
 * 发送email
 */
var emailSend=function(){
	var ischeck = 1;
	if(document.getElementById("ischeck")!=undefined){
		ischeck =2;
	};
	var email = $("#email").val();
	if(ischeck == 1){
		var imgcode = $("#imgcodeEmail").val();
		if(email==null || email.length==0) {
			showModal('请输入正确格式邮箱地址');
			$("#email").addClass("input-error");
			return false;
		} else if(email.length>0) {
			$("#email").removeClass("input-error");
		} 
		if(imgcode==null || imgcode.length==0) {
			showModal('请填写图形验证码');
			$("#imgcodeEmail").addClass("input-error");
			return false;
		} else if(imgcode.length==5) {
			$("#imgcodeEmail").removeClass("input-error");
		};
	}else{
		if(email==null || email.length==0) {
			showModal('请先设置邮箱');
			return false;
		}; 
	};
	var smsurl=$("#sendEmailUrl").val()+"?email="+email+"&imgcode="+imgcode+"&imgCodeId="+$("#emlRegImgCodeId").val();;
	$.ajax({
		type:'get',
		url:smsurl,
		success:function(data){
			if(data.code=='error') {
				showModal(data.info);
			} else {
				fun_timedownE(60);
				showModal(data.info);
				$("#sendEmailMess").addClass("button-disable");
				$("#sendEmailMess").unbind("click");
			}
		}
	});
}

function fun_timedownE(time) {
	$("#sendEmailMess").text(time+"秒");
    time = time-1;
    if(time>=0){
        setTimeout("fun_timedownE("+time+")",1000);
    }else{
    	$("#sendEmailMess").removeClass("button-disable");
    	$("#sendEmailMess").click(function(){
			emailSend();
    	});
    	$("#sendEmailMess").text("发送验证码");
    }
}